package com.xckj.teacher.ui.fragment;

import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.recyclerview.widget.GridLayoutManager;

import com.xckj.teacher.R;
import com.xckj.teacher.bean.HomeBean;
import com.xckj.teacher.bean.TextbookBean;
import com.xckj.teacher.bean.UnitBean;
import com.xckj.teacher.databinding.FragmentHomeBinding;
import com.xckj.teacher.framework.BaseFragment;
import com.xckj.teacher.global.Constant;
import com.xckj.teacher.iview.IHomeView;
import com.xckj.teacher.presenter.HomePresenter;
import com.xckj.teacher.ui.activity.BrowserActivity;
import com.xckj.teacher.ui.activity.SearchBookActivity;
import com.xckj.teacher.ui.adapter.HomeCollectionListAdapter;
import com.xckj.teacher.util.CacheUtil;
import com.xckj.teacher.util.LoadImageUtil;
import com.xckj.teacher.view.SelectedTextbookDialog;
import com.xckj.teacher.view.SelectedUnitDialog;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.DrawableIndicator;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @Author: ChenYao
 * @Describe: 首页
 * @Date: 2024/4/10 22:42
 */
public class HomeFragment extends BaseFragment<FragmentHomeBinding> implements IHomeView {
    private HomePresenter homePresenter;
    private HomeCollectionListAdapter homeCollectionListAdapter;
    private List<TextbookBean> mTextbookBeans;
    private String unitId;
    private String versionId;

    @Override
    protected int loadLayout() {
        return R.layout.fragment_home;
    }

    @Override
    protected int setStatusBarColor() {
        return R.color.home_top_status;
    }

    @Override
    protected boolean isStatusBarDarkFont() {
        return false;
    }

    @Override
    protected void initData() {
        dataBinding.tvTitle.setText(R.string.tab_home);
        homePresenter = new HomePresenter(this);
        homePresenter.getTextbook();
        initCollectionList();
        setListener();
    }

    @Override
    public void onResume() {
        super.onResume();
        homePresenter.getHome();
    }

    @Override
    public void success(HomeBean homeBean) {
        initBanner(homeBean.banner);
        setCollect(homeBean);
        setBookSync(homeBean);
    }

    @Override
    public void success(List<TextbookBean> textbookBeans) {
        mTextbookBeans = textbookBeans;
    }

    @Override
    public void successUnit(List<UnitBean> unitBeans) {
        showUnitDialog(unitBeans);
    }

    @Override
    public void success() {
        homePresenter.getHome();
    }


    private void setBookSync(HomeBean homeBean) {
      if (null!=homeBean.unit){
          dataBinding.tvBookSync.setText(homeBean.unit.book_name);
          dataBinding.tvUnitName.setText(homeBean.unit.name);
          unitId = homeBean.unit.unitid;
          versionId = homeBean.unit.versionId;
      }else {
          dataBinding.tvBookSync.setText("请选择教材");
      }
    }

    private void setCollect(HomeBean homeBean) {
        if (0 != homeBean.collect.size()) {
            dataBinding.llCollectionList.setVisibility(View.VISIBLE);
            homeCollectionListAdapter.setCollectsData(homeBean.collect);
        } else {
            dataBinding.llCollectionList.setVisibility(View.GONE);
        }
    }

    /**
     * 初始化Banner
     *
     * @param bannerBean Banner的数据
     */
    private void initBanner(List<HomeBean.Banner> bannerBean) {
        dataBinding.honeBanner.addBannerLifecycleObserver(this);
        dataBinding.honeBanner.setIndicator(new DrawableIndicator(getContext(), R.mipmap.icon_banner_normal, R.mipmap.icon_banner_selected));
        BannerImageAdapter<HomeBean.Banner> bannerImageAdapter = new BannerImageAdapter<HomeBean.Banner>(bannerBean) {
            @Override
            public void onBindView(BannerImageHolder holder, HomeBean.Banner data, int position, int size) {
                LoadImageUtil.loadImage(data.img_full, holder.imageView, R.mipmap.img_default_banner);
            }
        };
        dataBinding.honeBanner.setAdapter(bannerImageAdapter);
        OnBannerListener bannerListener = (data, position) -> {

        };
        dataBinding.honeBanner.setOnBannerListener(bannerListener);
    }

    private void initCollectionList() {
        dataBinding.recCollectionList.setLayoutManager(new GridLayoutManager(requireActivity(), 3));
        homeCollectionListAdapter = new HomeCollectionListAdapter();
        homeCollectionListAdapter.setOnItemClickListener((view, position) -> {

        });
        dataBinding.recCollectionList.setAdapter(homeCollectionListAdapter);
    }


    @Override
    public void failed(String message) {
        showMessage(message);
    }

    private void setListener() {
        dataBinding.rlSearch.setOnClickListener(view -> {
            startActivity(new Intent(mContext, SearchBookActivity.class));
        });
        dataBinding.llBookSync.setOnClickListener(view -> {
            showGradeDialog();
        });
        dataBinding.llChangeUnit.setOnClickListener(view -> {
           if (!TextUtils.isEmpty(versionId)){
               homePresenter.getUnit(versionId);
           }else {
               showMessage("请选择教材");
           }
        });
        dataBinding.rlBookSync.setOnClickListener(view -> {
            if (!TextUtils.isEmpty(versionId)){
                BrowserActivity.start(requireActivity(),String.format(Constant.HOME_BOOK_SYNC, unitId,versionId,CacheUtil.INSTANCE.getToken()));
                Log.e("CY_", String.format(Constant.HOME_BOOK_SYNC, unitId,versionId,CacheUtil.INSTANCE.getToken()));
            }else {
                showMessage("请选择教材");
            }
        });
        dataBinding.llHear.setOnClickListener(view -> {
            BrowserActivity.start(requireActivity(), String.format(Constant.HOME_H5_URL, CacheUtil.INSTANCE.getToken(), getString(R.string.hear)));
        });
        dataBinding.llHearing.setOnClickListener(view -> {
            BrowserActivity.start(requireActivity(), String.format(Constant.HOME_H5_URL, CacheUtil.INSTANCE.getToken(), getString(R.string.hearing)));
        });
        dataBinding.llReading.setOnClickListener(view -> {
            BrowserActivity.start(requireActivity(), String.format(Constant.HOME_H5_URL, CacheUtil.INSTANCE.getToken(), getString(R.string.reading)));
        });
        dataBinding.llWrittenTest.setOnClickListener(view -> {
            BrowserActivity.start(requireActivity(), String.format(Constant.HOME_H5_URL, CacheUtil.INSTANCE.getToken(), getString(R.string.written_test)));
        });
        dataBinding.llPhoneticAlphabet.setOnClickListener(view -> {
            BrowserActivity.start(requireActivity(), String.format(Constant.HOME_H5_URL, CacheUtil.INSTANCE.getToken(), getString(R.string.phonetic_alphabet)));
        });
        dataBinding.llModel.setOnClickListener(view -> {
            BrowserActivity.start(requireActivity(), String.format(Constant.HOME_H5_URL, CacheUtil.INSTANCE.getToken(), getString(R.string.model)));
        });
        dataBinding.llNewConcept.setOnClickListener(view -> {
            BrowserActivity.start(requireActivity(), String.format(Constant.HOME_H5_URL, CacheUtil.INSTANCE.getToken(), getString(R.string.new_concept)));
        });
        dataBinding.llBlackPlum.setOnClickListener(view -> {
            BrowserActivity.start(requireActivity(), String.format(Constant.HOME_H5_URL, CacheUtil.INSTANCE.getToken(), getString(R.string.black_plum)));
        });
        dataBinding.llInterest.setOnClickListener(view -> {
            BrowserActivity.start(requireActivity(), String.format(Constant.HOME_H5_URL, CacheUtil.INSTANCE.getToken(), getString(R.string.interest)));
        });
        dataBinding.llLook.setOnClickListener(view -> {
            BrowserActivity.start(requireActivity(), String.format(Constant.HOME_H5_URL, CacheUtil.INSTANCE.getToken(), getString(R.string.look)));
        });
    }

    private void showGradeDialog() {
        SelectedTextbookDialog selectedTextbookDialog = new SelectedTextbookDialog(requireActivity(), R.style.CustomDialogStyle);
        Window window = selectedTextbookDialog.getWindow();
        WindowManager.LayoutParams localLayoutParams = Objects.requireNonNull(window).getAttributes();
        localLayoutParams.gravity = Gravity.BOTTOM;
        window.setGravity(Gravity.BOTTOM);
        window.getDecorView().setPadding(0, 0, 0, 0);
        localLayoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        localLayoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        selectedTextbookDialog.getWindow().setAttributes(localLayoutParams);
        selectedTextbookDialog.setData(mTextbookBeans);
        selectedTextbookDialog.setCloseClickListener(new SelectedTextbookDialog.OnCloseClickListener() {
            @Override
            public void onCloseClick() {
                selectedTextbookDialog.dismiss();
            }
        });
        selectedTextbookDialog.setOnItemClickListener(new SelectedTextbookDialog.OnItemClickListener() {
            @Override
            public void onItemClick(TextbookBean.Children children) {
                selectedTextbookDialog.dismiss();
                homePresenter.getUnit(children.versionId);
            }
        });
        selectedTextbookDialog.show();
    }

    private void showUnitDialog(List<UnitBean> unitBeans) {
        SelectedUnitDialog selectedUnitDialog = new SelectedUnitDialog(requireActivity(), R.style.CustomDialogStyle);
        Window window = selectedUnitDialog.getWindow();
        WindowManager.LayoutParams localLayoutParams = Objects.requireNonNull(window).getAttributes();
        localLayoutParams.gravity = Gravity.BOTTOM;
        window.setGravity(Gravity.BOTTOM);
        window.getDecorView().setPadding(0, 0, 0, 0);
        localLayoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        localLayoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        selectedUnitDialog.getWindow().setAttributes(localLayoutParams);
        selectedUnitDialog.setData(unitBeans);
        selectedUnitDialog.setCloseClickListener(new SelectedUnitDialog.OnCloseClickListener() {
            @Override
            public void onCloseClick() {
                selectedUnitDialog.dismiss();
            }
        });
        selectedUnitDialog.setOnItemClickListener((unitId, versionId) -> {
           homePresenter.saveUnit(unitId,versionId);
           selectedUnitDialog.dismiss();
        });
        selectedUnitDialog.show();
    }
}
